/*
 * @Description:
 * @Version: 2.0
 * @Autor: Seven
 * @Date: 2023-12-12 09:08:37
 * @LastEditors: Seven
 * @LastEditTime: 2023-12-18 17:05:15
 */
import React, { memo, useEffect, useState } from "react";
import { Modal, Table, Button } from "antd";
import axios from "axios";
import DetailModal from "./DetailModal";

const InfoModal = memo(({ infoModal, handleInfoCancel, infoRecode }) => {
  const [tableIInfoData, setTableIInfoData] = useState([]);
  const [tableDtaaInfoDetail, setTableDtaaInfoDetail] = useState(null);
  const [detailModalVisible, setDetailModalVisible] = useState(false);
  useEffect(() => {
    axios({
      url: `http://192.168.200.129:8081/wordDoc/listRecord/${infoRecode.id}`,
      timeout: 30000,
    })
      .then((res) => {
        if (res.data.code === 200) {
          setTableIInfoData(res.data.data);
        }
      })
      .catch((err) => {
        console.log(err);
      });
  }, [infoRecode]);
  const detailHandleCancel = () => {
    setDetailModalVisible(false);
  };
  const columns = [
    {
      title: "名称",
      dataIndex: "name",
      key: "name",
      align: "center",
    },
    {
      title: "分类",
      dataIndex: "cate",
      key: "cate",
      align: "center",
    },
    {
      title: "创建时间",
      dataIndex: "createTime",
      key: "createTime",
      align: "center",
    },
    {
      title: "操作",
      dataIndex: "operator",
      key: "operator",
      align: "center",
      render: (text, recode) => {
        return (
          <>
            <Button
              type="primary"
              style={{ margin: "10px" }}
              onClick={(e) => {
                detailModal(recode);
              }}
            >
              详情
            </Button>
            <Button
              type="primary"
              onClick={(e) => {
                exportBtn(recode);
              }}
            >
              导出
            </Button>
          </>
        );
      },
    },
  ];
  const detailModal = (recode) => {
    axios({
      url: `http://192.168.200.129:8081/wordDoc/getRecord/${recode.id}`,
      timeout: 30000,
    })
      .then((res) => {
        if (res.data.code === 200) {
          setTableDtaaInfoDetail(res.data.data);
          setDetailModalVisible(true);
        }
      })
      .catch((err) => {
        console.log(err);
      });
  };
  const exportBtn = (record) => {
    window.open(
      `http://192.168.200.129:8081/wordDoc/export/${record.id}`,
      "_blank"
    );
  };
  return (
    <div>
      <Modal
        title="查看记录"
        open={infoModal}
        onOk={handleInfoCancel}
        onCancel={handleInfoCancel}
        width="760px"
      >
        <Table
          dataSource={tableIInfoData}
          columns={columns}
          pagination={false}
          rowKey="id"
        />
      </Modal>
      {detailModalVisible && (
        <DetailModal
          detailModalVisible={detailModalVisible}
          detailHandleCancel={detailHandleCancel}
          tableDtaaInfoDetail={tableDtaaInfoDetail}
        />
      )}
    </div>
  );
});

export default InfoModal;
